package com.test.vote.controller;

import com.test.vote.domain.BattlePlayer;
import com.test.vote.pojo.Battle;
import com.test.vote.pojo.Player;
import com.test.vote.response.CodeMsg;
import com.test.vote.response.ResultData;
import com.test.vote.service.battleService;
import com.test.vote.service.playerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class battleController {

    @Autowired
    battleService battleService;
    @Autowired
    playerService playerService;

    //比赛管理员添加对战
    @PostMapping("/addBattle")
    @ResponseBody
    public ResultData addBattle(Battle battle) {
        if (battleService.addBattle(battle) > 0) {
            return ResultData.success(true);
        }
        return ResultData.error(CodeMsg.SERVER_ERROR);
    }

    //比赛管理员开启对战，开始投票
    @PostMapping("/beginBattle")
    @ResponseBody
    public ResultData beginBattle(Battle battle) {
        if (battleService.updateBattleStatus(battle) > 0) {
            return ResultData.success(true);
        }
        return ResultData.error(CodeMsg.SERVER_ERROR);
    }

    //比赛管理员暂停对战，暂停投票
    @PostMapping("/stopBattle")
    @ResponseBody
    public ResultData stopBattle(Battle battle) {
        if (battleService.updateBattleStatus(battle) > 0) {
            return ResultData.success(true);
        }
        return ResultData.error(CodeMsg.SERVER_ERROR);
    }

    //比赛管理员终止对战,停止投票
    @PostMapping("/endBattle")
    @ResponseBody
    public ResultData endBattle(Battle battle) {
        if (battleService.updateBattleStatus(battle) > 0) {
            return ResultData.success(true);
        }
        return ResultData.error(CodeMsg.SERVER_ERROR);
    }

    //比赛管理员通过对战的Id展示对战投票页面
    @RequestMapping("/showVote")
    public String showVote(int battleId, Model model) {
        Battle battle = battleService.selectBattleById(battleId);
        Player playera = playerService.selectPlayerById(battle.getPlayeraId());
        Player playerb = playerService.selectPlayerById(battle.getPlayerbId());

        BattlePlayer battlePlayer = new BattlePlayer();
        battlePlayer.setBattleId(battle.getBattleId());
        battlePlayer.setMatchId(battle.getMatchId());
        battlePlayer.setPlayeraImg(playera.getPlayerImg());
        battlePlayer.setPlayeraName(playera.getPlayerName());
        battlePlayer.setPlayeraNumber(playera.getPlayerNumber());
        battlePlayer.setPlayeraSong(battle.getPlayeraSong());
        battlePlayer.setPlayeraTicket(battle.getPlayeraTicket());
        battlePlayer.setPlayerbImg(playerb.getPlayerImg());
        battlePlayer.setPlayerbNumber(playerb.getPlayerNumber());
        battlePlayer.setPlayerbName(playerb.getPlayerName());
        battlePlayer.setPlayerbSong(battle.getPlayerbSong());
        battlePlayer.setPlayerbTicket(battle.getPlayerbTicket());
        battlePlayer.setBattleStatus(battle.getBattleStatus());

        model.addAttribute("battlePlayer", battlePlayer);
        return "matchAdmin/vote";
    }

    //用户通过对战的Id展示对战投票页面
    @RequestMapping("/userVote")
    public String userVote(int battleId, Model model) {
        Battle battle = battleService.selectBattleById(battleId);
        Player playera = playerService.selectPlayerById(battle.getPlayeraId());
        Player playerb = playerService.selectPlayerById(battle.getPlayerbId());

        BattlePlayer battlePlayer = new BattlePlayer();
        battlePlayer.setBattleId(battle.getBattleId());
        battlePlayer.setMatchId(battle.getMatchId());
        battlePlayer.setPlayeraImg(playera.getPlayerImg());
        battlePlayer.setPlayeraName(playera.getPlayerName());
        battlePlayer.setPlayeraNumber(playera.getPlayerNumber());
        battlePlayer.setPlayeraSong(battle.getPlayeraSong());
        battlePlayer.setPlayeraTicket(battle.getPlayeraTicket());
        battlePlayer.setPlayerbImg(playerb.getPlayerImg());
        battlePlayer.setPlayerbNumber(playerb.getPlayerNumber());
        battlePlayer.setPlayerbName(playerb.getPlayerName());
        battlePlayer.setPlayerbSong(battle.getPlayerbSong());
        battlePlayer.setPlayerbTicket(battle.getPlayerbTicket());
        battlePlayer.setBattleStatus(battle.getBattleStatus());
        battlePlayer.setPlayeraId(playera.getPlayerId());
        battlePlayer.setPlayerbId(playerb.getPlayerId());
        model.addAttribute("battlePlayer", battlePlayer);
        return "user/vote";
    }

}
